# app/utils.py

import smtplib
from email.mime.text import MIMEText
from .config import config  # 修改这里
import logging

logger = logging.getLogger(__name__)

def send_email(subject: str, body: str, recipient: str):
    msg = MIMEText(body, 'plain', 'utf-8')
    msg['Subject'] = subject
    msg['From'] = config.SMTP_USERNAME
    msg['To'] = recipient

    try:
        with smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) as server:
            server.starttls()
            server.login(config.SMTP_USERNAME, config.SMTP_PASSWORD)
            server.sendmail(config.SMTP_USERNAME, [recipient], msg.as_string())
        logger.info(f"邮件已发送至 {recipient}")
    except Exception as e:
        logger.error(f"发送邮件失败: {e}")
